package com.shangcheng.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.common.Result;
import com.shangcheng.entity.TCustomer;
import com.shangcheng.service.TCustemerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tCustomer")
public class TCustomerController {

    @Autowired
    private TCustemerServiceImpl service;

    // 获取个人信息
    @GetMapping("/info")
    public Result<Map<String, Object>> getCustomerInfo(@RequestParam String username) {
        Result<Map<String, Object>> re = Result.ok((Map<String, Object>) null);
        TCustomer customer = service.queryByUsername(username);
        if (customer == null) {
            re.setCode(100);
            re.setMessage("用户不存在！");
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("id", customer.getId());
            map.put("userName", customer.getUserName());
            map.put("password", customer.getPassword()); // 添加密码字段
            map.put("name", customer.getName());
            map.put("sex", customer.getSex());
            map.put("age", customer.getAge());
            map.put("address", customer.getAddress());
            map.put("idCard", customer.getIdCard());
            map.put("status", customer.getStatus());
            map.put("createTime", customer.getCreateTime());
            re.setData(map);
            re.setMessage("获取个人信息成功！");
        }
        return re;
    }

    // 更新个人信息
    @PutMapping("/info")
    public Result<Map<String, Object>> updateCustomerInfo(@RequestBody TCustomer customer) {
        Result<Map<String, Object>> re = Result.ok((Map<String, Object>) null);
        TCustomer existingCustomer = service.queryByUsername(customer.getUserName());
        if (existingCustomer == null) {
            re.setCode(100);
            re.setMessage("用户不存在！");
        } else {
            // 更新信息
            if (customer.getPassword() != null) {
                existingCustomer.setPassword(customer.getPassword());
            }
            if (customer.getName() != null) {
                existingCustomer.setName(customer.getName());
            }
            if (customer.getSex() != null) {
                existingCustomer.setSex(customer.getSex());
            }
            if (customer.getAge() != null) {
                existingCustomer.setAge(customer.getAge());
            }
            if (customer.getAddress() != null) {
                existingCustomer.setAddress(customer.getAddress());
            }
            if (customer.getIdCard() != null) {
                existingCustomer.setIdCard(customer.getIdCard());
            }
            existingCustomer.setStatus(customer.getStatus());

            // 调用更新方法
            service.updateCustomer(existingCustomer);

            Map<String, Object> map = new HashMap<>();
            map.put("message", "个人信息更新成功！");
            re.setData(map);
            re.setMessage("个人信息更新成功！");
        }
        return re;
    }
    @GetMapping("/listc")
    public Result<Page<TCustomer>> findCustomers(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "15") int size){
        Page<TCustomer> customerPage = service.pageCustomers(page,size);
        return Result.ok(customerPage);
    }
    @DeleteMapping("listcD/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        service.deleteCustomerById(id);
        return Result.ok("ok");
    }

}
